@extends('layouts.admin.header')
@section('cnt')
<div class="layui-fluid">
    <div class="layui-row">

        <form class="layui-form">
            @csrf
            <div class="layui-form-item">
                <label class="layui-form-label">父级分类</label>
                <div class="layui-input-block" style="width: 190px;">
                    <select name="pid" lay-verify="required" lay-filter="menutree-dropdown">
                        <option value="0">ROOT</option>
                        @foreach($data as $k=>$v)
                            <option value="{{$v['id']}}">{{$v['name']}}</option>
                        @endforeach
                    </select>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>请选择父级菜单
                    </div>
                </div>

            </div>
            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>分类名称
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>请输入分类名称
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    增加
                </button>
            </div>
        </form>

    </div>
</div>
@endsection
@section('js')
    <script type="text/javascript" src="{{asset('admin/js/jquery.min.js')}}"></script>

    <script>
    layui.use(['form', 'layer','upload'],
        function() {
            var   $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            upload = layui.upload;
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '{{route('admin.goods_classes.goods_class_upload')}}' //改成您自己的上传接口
                ,data:{'_token':'{{csrf_token()}}'}
                ,before: function(obj){
                    console.log(obj)
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        console.log(index)
                        $('#pic').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(res){
                        console.log(res)
                    //如果上传失败
                    if(res.status < 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $('#thumb').val(res.url);
                }
                // ,error: function(){
                //     //演示失败状态，并实现重传
                //     var demoText = $('#demoText');
                //     demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                //     demoText.find('.demo-reload').on('click', function(){
                //         uploadInst.upload();
                //     });
                // }
            });
            //监听提交
            form.on('submit(add)', function(data) {
                let url='{{route('admin.goodsclasses.store')}}';
                let formData = data.field;
                $.post(url,formData).then(({status,msg})=>{
                    if(status==200){
                        //发异步，把数据提交给php
                        layer.alert("增加成功", {
                                icon: 6
                            },
                            function() {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    }else{
                        layer.alert("增加失败", {
                                icon: 5
                            },
                            function() {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    }
                });
                return false;
                    });


        });
</script>
@endsection
